home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1998 / MacHack 1998.toast / The Hacks! / Talking KeyBoard / Source / AppleEvents / aeaccessors.c next >
Encoding:
Text File  |  1998-05-08  |  4.9 KB  |  166 lines  |  [TEXT/CWIE]

  1. // Program Author: Paul Baxter
  2. //    pbaxter@assistivetech.com
  3. //
  4. //
  5.  
  6. #include "aeutils.h"
  7. #include "aetoken.h"
  8. #include "aeaccessors.h"
  9.  
  10.  
  11. // * ****************************************************************************** *
  12. // *            InstallAccessors
  13. // *                                Install AppleEvent Accessors 
  14. // * ****************************************************************************** *
  15. OSErr    InstallAccessors(void)
  16. {
  17.     OSErr    err;
  18.  
  19.     err = AEInstallObjectAccessor(cProperty,    typeNull,             NewOSLAccessorProc(PropertyFromNullAccessor), 0, false);
  20.     err = AEInstallObjectAccessor(cProperty,    typeMyApplProp,     NewOSLAccessorProc(PropertyFromApplAccessor), 0, false);
  21.     err = AEInstallObjectAccessor(cProperty,    typeMyAppl,         NewOSLAccessorProc(PropertyFromApplAccessor), 0, false);
  22.     err = AEInstallObjectAccessor(cApplication, typeNull,            NewOSLAccessorProc(ApplicationFromNullAccessor), 0, false);
  23.  
  24.     return(err);
  25. }
  26.  
  27.  
  28. // * ****************************************************************************** *
  29. // *            ApplicationFromNullAccessor
  30. // *                                Get Application Desc from nothing 
  31. // * ****************************************************************************** *
  32. pascal OSErr   ApplicationFromNullAccessor(DescType        wantClass,
  33.                                             const AEDesc    *container,
  34.                                             DescType        containerClass,
  35.                                             DescType        form, 
  36.                                             const AEDesc    *selectionData,
  37.                                             AEDesc            *value,
  38.                                             long            theRefCon)
  39. {
  40. #pragma unused(container,selectionData,theRefCon)
  41.  
  42.     OSErr    myErr;
  43.     AppToken theApp;
  44.     AEDesc   resultDesc;
  45.  
  46.     
  47.     value->dataHandle     = nil;
  48.     resultDesc.dataHandle = nil;
  49.     
  50.     
  51.     if ((wantClass != cApplication) || (containerClass != typeNull) ||
  52.           !((form == formName) || (form == formAbsolutePosition)))
  53.         return(errAEWrongDataType);
  54.     
  55.     if ((form == formName) || (form == formAbsolutePosition)) {
  56.         theApp.highLongOfPSN = 0;
  57.         theApp.lowLongOfPSN  = kCurrentProcess;
  58.     }
  59.         
  60.     myErr = AECreateDesc(typeMyAppl, (Ptr)&theApp, sizeof(theApp), value);
  61.             
  62.     return(myErr);
  63. }    /* ApplicationFromNullAccessor */
  64.  
  65.  
  66.  
  67. // * ****************************************************************************** *
  68. // *            PropertyFromNullAccessor
  69. // *                                Get Application Property Desc from nothing 
  70. // * ****************************************************************************** *
  71. pascal OSErr PropertyFromNullAccessor(DescType            wantClass,
  72.                                             AEDesc            *container,
  73.                                             DescType        containerClass,
  74.                                             DescType        form, 
  75.                                             AEDesc            *selectionData,
  76.                                             AEDesc            *value,
  77.                                             long            theRefCon)
  78. {
  79. #pragma unused (container, containerClass)
  80.  
  81.     AEDesc        aDesc = {typeNull, NULL};
  82.     OSErr        err;
  83.  
  84.  
  85.     if ((wantClass != cProperty) || (form != formPropertyID))
  86.         return(errAEWrongDataType);
  87.  
  88.     switch (*(DescType *)*selectionData->dataHandle) {
  89.         
  90.         case typeMyApplProp:
  91.             err = PropertyFromApplAccessor(wantClass, &aDesc, typeMyAppl, form, 
  92.                                                     selectionData, value, theRefCon);
  93.             break;
  94.  
  95.         default:            // Otherwise try an application property - it is from NULL
  96.             err = PropertyFromApplAccessor(wantClass, &aDesc, typeMyAppl, form, 
  97.                                                     selectionData, value, theRefCon);
  98.     }
  99.     
  100. done:
  101.     if (aDesc.dataHandle)
  102.         AEDisposeDesc(&aDesc);
  103.  
  104.     return(err);
  105. }
  106.  
  107. // * ****************************************************************************** *
  108. // *            PropertyFromApplAccessor
  109. // *                                Get Application Property Desc from nothing 
  110. // * ****************************************************************************** *
  111.  
  112. pascal OSErr PropertyFromApplAccessor(DescType            wantClass,
  113.                                         const AEDesc    *container,
  114.                                         DescType        containerClass,
  115.                                         DescType        form, 
  116.                                         const AEDesc    *selectionData,
  117.                                         AEDesc            *value,
  118.                                         long            theRefCon)
  119. {
  120. #pragma unused (theRefCon, containerClass)
  121.  
  122.     OSErr         myErr;
  123.     OSErr         ignoreErr;
  124.     AppToken      theApplToken;
  125.     DescType      theProperty;
  126.     AEDesc        applDesc;
  127.     AEDesc        propDesc;
  128.     Size          actualSize;
  129.     ApplPropToken myApplProp;
  130.  
  131.     value->dataHandle     = nil;
  132.     applDesc.dataHandle   = nil;
  133.     propDesc.dataHandle   = nil;
  134.     
  135.     if ((wantClass != cProperty) ||
  136.           (form != formPropertyID)) {
  137.             return(errAEWrongDataType);
  138.     }
  139.     
  140.     /* get the application token - it's the container */
  141.     
  142.     myErr = AECoerceDesc(container, typeMyAppl, &applDesc);
  143.     GetRawDataFromDescriptor(&applDesc, (Ptr)&theApplToken, sizeof(theApplToken), &actualSize);
  144.             
  145.     /* get the property - it's in the selection data */
  146.     myErr = AECoerceDesc(selectionData, typeType, &propDesc);
  147.     GetRawDataFromDescriptor(&propDesc, (Ptr)&theProperty, sizeof(theProperty), &actualSize);
  148.     /*
  149.         Combine the two into single token
  150.     */
  151.  
  152.     myApplProp.tokenApplToken    = theApplToken;
  153.     myApplProp.tokenApplProperty = theProperty;
  154.     
  155.     myErr = AECreateDesc(typeMyApplProp, (Ptr)&myApplProp, sizeof(myApplProp), value);
  156.         
  157.     if (applDesc.dataHandle)
  158.         ignoreErr = AEDisposeDesc(&applDesc);
  159.         
  160.     if (propDesc.dataHandle)
  161.         ignoreErr = AEDisposeDesc(&propDesc);
  162.         
  163.     return(myErr);
  164. } // PropertyFromApplAccessor
  165.  
  166.